package com.wsd.web;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cif.service.CustomerService;
import com.wsd.common.Result;
import com.wsd.constant.ResultCode;
import com.cif.domain.Customer;
import com.wsd.domain.LoanInfo;
import com.wsd.domain.LoanInformation;
import com.wsd.service.LoanListService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname LoanListController
 * @Description TODO
 * @Date 2020/7/23 12:08
 * @Created by niuxiaojuan
 */
@RestController
@RequestMapping("/manage")
public class LoanListController extends BaseController{
    @Autowired
    LoanListService loanListService;

    @Reference(version = "1.0.0", url = "${dubbo.service.cifurl}")
    private CustomerService customerService;

    @RequestMapping("/getIngLoanList")
    public  Map getIngLoanList(@RequestParam("pagenum") int pagenum, @RequestParam("num") int num)
    {
        Map<String, Object> map = new HashMap<String, Object>();
            List<LoanInfo> list=null;
            try {
                list = loanListService.findIngLoanList(pagenum, num);
                for (LoanInfo l : list) {
                    Customer c = JSONObject.parseObject(JSONObject.toJSONString(changeResult(customerService.getCustomerById(l.getCustomer_id().toString())).getData()),Customer.class);
                    l.setUserName(c.getUserName());
                    l.setIdNumber(c.getIdNumber());
                }

                map.put("success","true");
                map.put("resultCode", "000");
                map.put("resultMsg","OK");
                map.put("recordCount",loanListService.countIngLoanList());
                map.put("list",list);
            }catch(Exception e)
            {
                map.put("success","false");
                map.put("resultCode", "GETINGLOAN-ERROR");
                map.put("resultMsg","返回进行中贷款信息出错");
            }
            //mav.setViewName("getIngLoanList");
        return map;
    }

    @RequestMapping("/getEndLoanList")
    public Map getEndLoanList(@RequestParam("pagenum") int pagenum, @RequestParam("num") int num)
    {

        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<LoanInfo> list = loanListService.findEndLoanList(pagenum, num);
            for (LoanInfo l : list) {
                Customer c = JSONObject.parseObject(JSONObject.toJSONString(changeResult(customerService.getCustomerById(l.getCustomer_id().toString())).getData()),Customer.class);
                l.setUserName(c.getUserName());
                l.setIdNumber(c.getIdNumber());
            }
            map.put("success","true");
            map.put("resultCode", "000");
            map.put("resultMsg", "OK");
            map.put("recordCount",loanListService.countEndLoanList());
            map.put("list", list);
        }catch(Exception e)
        {
            map.put("success","false");
            map.put("resultCode", "GETENDLOAN-ERROR");
            map.put("resultMsg","返回完结贷款信息出错");
        }
        return map;
    }

}
